import 'package:flutter/material.dart';

/// 程序块类型枚举
enum BlockType {
  motion,    // 移动类
  control,   // 控制类
  event,     // 事件类
  operator,  // 运算类
  sensor,    // 传感器类
}

/// 程序块模型
class ProgramBlock {
  final String id;
  final String name;
  final BlockType type;
  final IconData icon;
  final Color color;
  final List<String> parameters;
  final Map<String, dynamic> defaultValues;
  
  // 子块（用于控制类块，如循环、条件等）
  List<ProgramBlock> children = [];
  
  // 下一个块（用于顺序执行）
  ProgramBlock? next;
  
  // 参数值
  Map<String, dynamic> parameterValues = {};

  ProgramBlock({
    required this.id,
    required this.name,
    required this.type,
    required this.icon,
    required this.color,
    this.parameters = const [],
    this.defaultValues = const {},
  }) {
    // 初始化参数值为默认值
    for (var param in parameters) {
      parameterValues[param] = defaultValues[param];
    }
  }
  
  // 克隆方法，用于拖放创建新块
  ProgramBlock clone() {
    return ProgramBlock(
      id: id,
      name: name,
      type: type,
      icon: icon,
      color: color,
      parameters: List.from(parameters),
      defaultValues: Map.from(defaultValues),
    );
  }
}

/// 预定义的程序块
class ProgramBlocks {
  // 移动类块
  static final ProgramBlock moveForward = ProgramBlock(
    id: 'move_forward',
    name: '前进',
    type: BlockType.motion,
    icon: Icons.arrow_upward,
    color: Colors.blue,
    parameters: ['距离'],
    defaultValues: {'距离': 10},
  );
  
  static final ProgramBlock moveBackward = ProgramBlock(
    id: 'move_backward',
    name: '后退',
    type: BlockType.motion,
    icon: Icons.arrow_downward,
    color: Colors.blue,
    parameters: ['距离'],
    defaultValues: {'距离': 10},
  );
  
  static final ProgramBlock turnLeft = ProgramBlock(
    id: 'turn_left',
    name: '左转',
    type: BlockType.motion,
    icon: Icons.rotate_left,
    color: Colors.blue,
    parameters: ['角度'],
    defaultValues: {'角度': 90},
  );
  
  static final ProgramBlock turnRight = ProgramBlock(
    id: 'turn_right',
    name: '右转',
    type: BlockType.motion,
    icon: Icons.rotate_right,
    color: Colors.blue,
    parameters: ['角度'],
    defaultValues: {'角度': 90},
  );
  
  static final ProgramBlock changeAltitude = ProgramBlock(
    id: 'change_altitude',
    name: '改变高度',
    type: BlockType.motion,
    icon: Icons.height,
    color: Colors.blue,
    parameters: ['高度变化'],
    defaultValues: {'高度变化': 10},
  );
  
  // 控制类块
  static final ProgramBlock wait = ProgramBlock(
    id: 'wait',
    name: '等待',
    type: BlockType.control,
    icon: Icons.timer,
    color: Colors.orange,
    parameters: ['秒数'],
    defaultValues: {'秒数': 1},
  );
  
  static final ProgramBlock repeat = ProgramBlock(
    id: 'repeat',
    name: '重复',
    type: BlockType.control,
    icon: Icons.repeat,
    color: Colors.orange,
    parameters: ['次数'],
    defaultValues: {'次数': 10},
  );
  
  static final ProgramBlock ifThen = ProgramBlock(
    id: 'if_then',
    name: '如果...那么',
    type: BlockType.control,
    icon: Icons.call_split,
    color: Colors.orange,
    parameters: ['条件'],
    defaultValues: {'条件': ''},
  );
  
  // 事件类块
  static final ProgramBlock whenStart = ProgramBlock(
    id: 'when_start',
    name: '当开始时',
    type: BlockType.event,
    icon: Icons.play_arrow,
    color: Colors.green,
  );
  
  static final ProgramBlock whenAltitudeReaches = ProgramBlock(
    id: 'when_altitude_reaches',
    name: '当高度达到',
    type: BlockType.event,
    icon: Icons.height,
    color: Colors.green,
    parameters: ['高度'],
    defaultValues: {'高度': 50},
  );
  
  // 运算类块
  static final ProgramBlock lessThan = ProgramBlock(
    id: 'less_than',
    name: '小于',
    type: BlockType.operator,
    icon: Icons.keyboard_arrow_left,
    color: Colors.purple,
    parameters: ['值1', '值2'],
    defaultValues: {'值1': 0, '值2': 10},
  );
  
  static final ProgramBlock greaterThan = ProgramBlock(
    id: 'greater_than',
    name: '大于',
    type: BlockType.operator,
    icon: Icons.keyboard_arrow_right,
    color: Colors.purple,
    parameters: ['值1', '值2'],
    defaultValues: {'值1': 10, '值2': 0},
  );
  
  // 传感器类块
  static final ProgramBlock getAltitude = ProgramBlock(
    id: 'get_altitude',
    name: '获取高度',
    type: BlockType.sensor,
    icon: Icons.height,
    color: Colors.teal,
  );
  
  static final ProgramBlock getSpeed = ProgramBlock(
    id: 'get_speed',
    name: '获取速度',
    type: BlockType.sensor,
    icon: Icons.speed,
    color: Colors.teal,
  );
  
  static final ProgramBlock getBatteryLevel = ProgramBlock(
    id: 'get_battery_level',
    name: '获取电量',
    type: BlockType.sensor,
    icon: Icons.battery_full,
    color: Colors.teal,
  );
  
  // 获取所有可用块
  static List<ProgramBlock> getAllBlocks() {
    return [
      moveForward,
      moveBackward,
      turnLeft,
      turnRight,
      changeAltitude,
      wait,
      repeat,
      ifThen,
      whenStart,
      whenAltitudeReaches,
      lessThan,
      greaterThan,
      getAltitude,
      getSpeed,
      getBatteryLevel,
    ];
  }
  
  // 按类型获取块
  static List<ProgramBlock> getBlocksByType(BlockType type) {
    return getAllBlocks().where((block) => block.type == type).toList();
  }
} 